package com.mvc.interceptor;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 变种builder 模式，生产 MappedHandlerInterceptor
 */
public class InterceptorRegistration {
	private List<String> includedUrl=new ArrayList<>();   // 要进行拦截的url
	private List<String> excludedUrl=new ArrayList<>();;   // 要排除拦截的url
	
	
	private HandlerInterceptor interceptor;  // 实际的拦截器，外部传入
	
	
	public InterceptorRegistration(HandlerInterceptor interceptor) {
		this.interceptor = interceptor;
	}
	
	
	public InterceptorRegistration addIncludeUrl(String... url){
		return addIncludeUrl(Arrays.asList(url));
	}
	
	public InterceptorRegistration addIncludeUrl(List<String> url){
		includedUrl.addAll(url);
		return this;
	}
	
	public InterceptorRegistration addExcludeUrl(String... url){
		return addExcludeUrl(Arrays.asList(url));
	}
	
	public InterceptorRegistration addExcludeUrl(List<String> url){
		excludedUrl.addAll(url);
		return this;
	}
	
	public MappedHandlerInterceptor getInterceptor(){
		String[] include = includedUrl.toArray(new String[0]);
		String[] exclude = excludedUrl.toArray(new String[0]);
		
		return new MappedHandlerInterceptor(include, exclude, this.interceptor);
	}
}
